﻿using System;
using System.Collections.Generic;
using System.IO;
using CSJ2K.Util.IO;

namespace CSJ2K.Jpc.Segments
{
    /// <summary>Packed packet headers, tile-part header (PPT)</summary>
    /// <comment>
    /// Function: A collection of the packet headers from one tile or tile-part. 
    /// Usage: Tile-part headers. Shall appear in any tile-part header before the packets whose 
    ///      headers are described herein. The packet headers shall be in only one of three 
    ///      places within the codestream. If the PPM marker segment is present, all the packet 
    ///      headers shall be found in the main header. In this case, the PPT marker segment 
    ///      and packets distributed in the bit stream of the tile-parts are disallowed. If 
    ///      there is no PPM marker segment, then the packet headers can be distributed either 
    ///      in PPT marker segments or distributed in the codestream as defined in B.10. The 
    ///      packet headers shall not be in both a PPT marker segment and the codestream for 
    ///      the same tile. If the packet headers are in PPT marker segments, they shall appear 
    ///      in a tile-part header before the corresponding packet data appears (i.e., in the 
    ///      same tile-part header or one with a lower TPsot value). There may be multiple PPT 
    ///      marker segments in a tile-part header. 
    /// Length: Variable depending on the number of packets in each tile-part and the size of 
    ///      the packet headers.
    /// </comment>
    public class PPT : Segment
    {
        /// <summary>Index of this marker segment relative to all other PPT marker segments present in the current header.</summary>
        public byte Z;       

        /// <summary>Packet header for every packet in order in the tile-part.</summary>
        public byte[] I;

        public PPT(Stream source)
            : this(source, (Markers)StreamUtil.ReadBUInt16(source)) { }
        public PPT(Stream source, Markers marker)
            : base(source, marker, true)
        {
            if (marker != Markers.PPT) throw (new ApplicationException("Attempting to create segment of the wrong type."));

            Z = StreamUtil.ReadOctet(source);
            I = StreamUtil.ReadBytes(source, Length - 3);
        }
    }
}
